package com.woniuxy.service.Impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.woniuxy.dao.*;
import com.woniuxy.entity.*;
import com.woniuxy.service.LoanUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName LoanUserServiceImpl
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/20 15:30
 * @Version 1.0
 */
@Service
public class LoanUserServiceImpl implements LoanUserService {
    @Autowired
    LoanUserDao loanUserDao;
    @Autowired
    AssetDao assetDao;
    @Autowired
    CareerDao careerDao;
    @Autowired
    ContactDao contactDao;
    @Autowired
    LivingDao livingDao;
    /**
     * 添加用户具体信息
     * @param loanUserInfo
     * @return
     */
    @Override
    public AjaxResult addLoanUser(LoanUserInfo loanUserInfo){
        int userId = Math.toIntExact(SecurityUtils.getUserId());
        //查询当前用户是否存在信息，存在就更改，不存在就新增
        Integer id = loanUserDao.findLoanUserCount(userId);
        if(id!=null){
            int i= loanUserDao.updateLoanUser(loanUserInfo, id);
            if (i > 0) {
                return new AjaxResult(200, "success");
            } else {
                return new AjaxResult(500, "defeated");
            }
        }else {
            //查询当前登录用户，绑定用户表与信息表
            loanUserInfo.setUserId(Math.toIntExact(userId));
            //将资产信息、职业信息、联系人信息、住房信息id绑定到具体信息表
            AssetInfo assetId = assetDao.findFinalId();
            CareerInfo careerId = careerDao.findFinalId();
            ContactInfo contactId = contactDao.findFinalId();
            LivingInfo livingId = livingDao.findFinalId();
            loanUserInfo.setAssetInfoId(assetId);
            loanUserInfo.setCareerInfoId(careerId);
            loanUserInfo.setContactInfoId(contactId);
            loanUserInfo.setLivingInfoId(livingId);
            //将具体信息添加进具体信息表
            int i = loanUserDao.addLoanUser(loanUserInfo);
            if (i > 0) {
                return new AjaxResult(200, "success");
            } else {
                return new AjaxResult(500, "defeated");
            }
        }
    }

    @Override
    public AjaxResult findLoanUser() {
        int id = Math.toIntExact(SecurityUtils.getUserId());
        LoanUserInfo loanUserInfo = loanUserDao.findLoanUser(id);
        System.out.println(loanUserInfo.toString());
        return new AjaxResult(200,"success",loanUserInfo);
    }
}
